<?php

/**
 * NewsController
 * 
 * @author MF
 * @version 2.0
 */

require_once 'Zend/Controller/Action.php';

class Cms_NewsController extends Zend_Controller_Action {
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		CmsAuth::check();
		
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		
		$this->view->news_id = $id;
		
		$news = MF_Main::getInstance()->News->getById($id);
		$news_cfg = $news->config;
		$data_map = array();
		for ($g=0;$g<count($news_cfg);$g++) {
			array_push($data_map,$news_cfg[$g][3]);
		}
		$this->view->data_map = $data_map;
	}

	public function widgetAction() {
		CmsAuth::check();
		
		
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		
		$news = MF_Main::getInstance()->News->getById($id);
		
		
		
		if (NULL !== $act = $this->getRequest()->getParam('act')) {
			switch ($act) {
				case 'add':
					$news->add();
					break;
				
				case 'del':
					if (NULL === $note_id = $this->getRequest()->getParam('note_id')) {
						throw new Zend_Controller_Action_Exception('Required param "Note_Id" while del action precessed', 404);
					}
					$news->getById($note_id)->delete();
					break;
					
				default:
					;
				break;
			}
			
		}
		
		$news_list = $news->filter();
		
		if ((NULL !== $from_date = $this->getRequest()->getParam('from_date')) 
			&& (NULL !== $to_date = $this->getRequest()->getParam('to_date'))) {
				$news_list->dateFromTo($from_date,$to_date);
			}
		
		if (NULL !== $publish_f = $this->getRequest()->getParam('publish_f')) {
			$news_list->publishFlag($publish_f);
		}
		
	if ((NULL !== $limit_from = $this->getRequest()->getParam('limit_from')) 
			&& (NULL !== $limit_amount = $this->getRequest()->getParam('limit_amount'))) {
				$news_list->setLimits($limit_from,$limit_amount);
			}
		
		$news_list->Make();
		
		$newsNodes = array();
		for ($n=0;$n<$news_list->length();$n++) {
			$newsNote = $news_list->item($n);
			$newsNoteNode = 
				array(
					array('news_public',array('id' => $newsNote->_content_id,'value' => $newsNote->publish_f)),
					array('news_date',array('id' => $newsNote->_content_id,'value' => $newsNote->add_date)),
					array('news_title',array('id' => $newsNote->_content_id,'value' => $newsNote->title))
				);
				
			for ($c=0;$c<count($news->config);$c++) {
				$key_name = $news->config[$c][0]; 
				array_push($newsNoteNode,$this->_helper->WidgetAbstract($newsNote->$key_name));
			}

			array_push($newsNoteNode,array('news_delete',array('id' => $newsNote->_content_id)));
			
			array_push($newsNodes,$newsNoteNode);
		}
		
		
		$this->view->data = array('total_length' => $news_list->total_length(), 'data' => $newsNodes);
		$this->renderScript('json.phtml');
	}
	
	public function publishAction() {
		CmsAuth::check();
		
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		$newsNote = MF_Main::getInstance()->NewsNote->getById($id);
		$newsNote->publish_f = $newsNote->publish_f == '0' ? '1' : '0';
		$this->view->result = $newsNote->publish_f;
		$this->_helper->layout->disableLayout(); 
	}
	
	public function changeAction() {
		CmsAuth::check();
		
		$post = $this->_request->getPost();
		
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		$newsNote = MF_Main::getInstance()->NewsNote->getById($id);
		
		if (isset($post['title'])) {
			$title = $post['title']; 
			$newsNote->title = $title;
		}
		
	
		if (NULL !== $add_date = $this->getRequest()->getParam('add_date')) {
			$add_date = $add_date; 
			$newsNote->add_date = $add_date;
		}
		
		
		$this->view->result = '1';
		$this->_helper->layout->disableLayout(); 
	}
	
	
}
?>